package com.kevin.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @类名: ShellUtil
 * @包名：com.kevin.common.util
 * @时间：2017/10/25 15:14
 * @版本：1.0
 * @描述：Shell工具类
 */
public class ShellUtil {

    private static final Logger log = LoggerFactory.getLogger(ShellUtil.class);

    private ShellUtil() {
        // no constructor function
    }

    /**
     * 执行简单的shell命令
     * @param command
     * @throws IOException
     */
    public static void exec(String command) {
        try {
            log.info("执行shell命令: {}", command);
            Runtime.getRuntime().exec(command);
        } catch (IOException e) {
            log.error("执行shell命令失败: {}", command);
        }
    }

    /**
     * 执行复杂的shell命令
     * @param commands
     * @throws IOException
     */
    public static void exec(String... commands) {
        try {
            log.info("执行shell命令: {}", StringUtil.convert(commands, " "));
            Runtime.getRuntime().exec(commands);
        } catch (IOException e) {
            log.error("执行shell命令失败: {}",
                    StringUtil.convert(commands, " "));
        }
    }
}
